package thefirst.controller;

import java.sql.SQLException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import thefirst.LessonBoard.model.LessonBoardService;
import thefirst.LessonBoard.model.LessonBoardVO;
import thefirst.member.model.MemberVO;
import thefirst.member.model.StudentVO;
import thefirst.support.model.NoticeVO;

@Controller
public class LessonBoardController {
	@Resource(name = "lessonBoardServiceImpl")
	private LessonBoardService lessonBoardService;

	@RequestMapping("/writeLessonBoard.do")
	public String writeLessonBoard(LessonBoardVO lbvo, HttpSession session)
			throws SQLException {
		lessonBoardService.writeLessonBoard(lbvo);
		return "redirect:/showLessonBoardInfoNoHits.do?lessonno="+lbvo.getLessonno()+"&studentid="+lbvo.getStudentid();
	}

	/**
	 * 자신이 쓴 글을 조회
	 * 
	 * @param no
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/showLessonBoardInfoNoHits.do")
	public ModelAndView showLessonBoardInfoNoHits(LessonBoardVO lbvo) throws SQLException {
		//System.out.println("showLessonBoardInfoNoHits controller : "+no);
		return new ModelAndView(
				"LessonBoard/board/showLessonBoardInfo.lessonBoard",
				"lbvo", lessonBoardService.showLessonBoardInfoNoHits(lbvo));
	}

	/**
	 * 리스트를 보여준다.
	 * 
	 * @param pageNo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/lessonBoardSerch.do")
	public ModelAndView lessonBoardList(String pageNo, HttpSession session)
			throws SQLException {
		return new ModelAndView("LessonBoard/lessonBoardList.lessonBoard",
				"list", lessonBoardService.getLessonBoardList(pageNo));
	}

	/**
	 * 글쓰기 페이지로 이동
	 */
	@RequestMapping("/writeLessonPage.do")
	public String writeLessonBoardPage(LessonBoardVO lbvo, HttpServletRequest request,
			HttpServletResponse response) throws SQLException {
		return "LessonBoard/board/writeLessonPage.lessonBoard";
	}
	/**
	 * 카운터 올라간다~
	 * @param no
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/showLessonBoardInfo.do")
	public ModelAndView showLessonBoardInfo(LessonBoardVO lvo)throws SQLException{
		LessonBoardVO lbvo=lessonBoardService.showLessonInfo(lvo);
		return new ModelAndView("LessonBoard/board/showLessonBoardInfo.lessonBoard","lbvo",lbvo);
	}
	/**
	 * 삭제합니다.
	 * @param lvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/deleteLessonContent.do")
	public ModelAndView deleteLessonContent(LessonBoardVO lvo)throws SQLException{
		lessonBoardService.deleteLessonContent(lvo);
		return new ModelAndView("LessonBoard/lessonBoardList.lessonBoard","list",lessonBoardService.getLessonBoardList("1"));
	}
	@RequestMapping("/updateLessonPage.do")
	public ModelAndView updateLessonContent(LessonBoardVO lvo) throws SQLException{
		LessonBoardVO lbvo=lessonBoardService.showLessonInfo(lvo);
		return new ModelAndView("LessonBoard/board/updateLessonContent.lessonBoard","lbvo",lbvo);
	}
	@RequestMapping("/updateLesson.do")
	public String updateLesson(LessonBoardVO lvo) throws SQLException{
		System.out.println("controller:"+lvo);
		lessonBoardService.updateBoardContent(lvo);
		return "redirect:/showLessonBoardInfoNoHits.do?lessonno="+lvo.getLessonno()+"&studentid="+lvo.getStudentid();
	}
}
